package com.controller;

import com.pojo.Dept;
import com.pojo.Emp;
import com.services.DeptService;
import com.services.EmpService;

import org.apache.commons.beanutils.BeanUtils;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@WebServlet("/dept/*")
public class DeptServlet extends BaseServlet {
    private DeptService ds = new DeptService();

    public void index(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Dept dept = new Dept();
        List<Dept> depts = ds.queryIndex();
        req.getSession().setAttribute("DEPT_INDEX", depts);
        resp.sendRedirect(req.getContextPath() + "/deptManage.jsp");
    }

    public void addDept(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Dept dept = new Dept();
        req.setCharacterEncoding("utf-8");
        Map<String, String[]> map = req.getParameterMap();
        try {
            BeanUtils.populate(dept, map);
        } catch (IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        ds.addDept(dept);
        resp.sendRedirect(req.getContextPath() + "/dept/index");
    }

    public void removeDept(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Dept dept = new Dept();
        ds.removeDept(req.getParameter("id"));
        resp.sendRedirect(req.getContextPath() + "/dept/index");
    }

    //数据回写
//    public void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        List<Dept> dept = ds.editDept(req.getParameter("id"));
//        req.getSession().setAttribute("DEPT_EDIT", dept);
//        resp.sendRedirect(req.getContextPath()+"/dept/index");
//    }
    //数据持久化到数据库
    public void save(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String address = req.getParameter("address");

        ds.saveDept(id, name, address);
        resp.sendRedirect(req.getContextPath() + "/dept/index");
    }

    public void search(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String s = req.getParameter("searchs");
        List<Dept> arr = ds.querySearch(s);

        req.getSession().setAttribute("DEPT_INDEX", arr);
        resp.sendRedirect(req.getContextPath() + "/deptManage.jsp");
    }

    public void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        DeptService ds = new DeptService();
        int pages = (int) Math.ceil(ds.getPage() / (1.0 * 5));
        req.getSession().setAttribute("DEPT_ALL_PAGE", pages);
        req.getSession().setAttribute("DEPT_NOW_PAGE", 1);
        resp.sendRedirect("dept/index");
    }

    public void pagination(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pages = (int) req.getSession().getAttribute("DEPT_ALL_PAGE");
        String page = req.getParameter("page");
        int nowPage = Integer.parseInt(page);
        if (nowPage < 1) {
            nowPage = 1;
        }else if (nowPage > pages){
            nowPage = pages;
        }
        req.getSession().setAttribute("DEPT_NOW_PAGE", nowPage);
        DeptService ds = new DeptService();
        List<Dept> list = ds.queryIndex(nowPage);
        req.getSession().setAttribute("EMP_LIST", list);
//        req.getRequestDispatcher("/empManage.jsp").forward(req,resp);
        resp.sendRedirect(req.getContextPath() + "/deptManage.jsp");
    }
}
